<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改用户端消息提醒')" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-userMessage-edit" th:object="${zsyhUserMessage}">
            <input name="id" th:field="*{id}" type="hidden">
            <input name="userId" th:field="*{userId}" class="form-control" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">消息内容：</label>
                <div class="col-sm-8">
                    <textarea type="hidden" name="messageContent" class="form-control" rows="3" required>[[*{messageContent}]]</textarea>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">消息状态：</label>
                <div class="col-sm-8">
                    <select name="messageStatus" class="form-control m-b" th:with="type=${@dict.getType('message_type')}" required>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{messageStatus}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required" >消息类型：</label>
                <div class="col-sm-8">
                    <select id="messageType" onchange="stateValue()" name="messageType" class="form-control m-b" th:with="type=${@dict.getType('message_classify')}" required>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{messageType}"></option>
                    </select>
                </div>
            </div>
            <div ID="userNicknameDiv" class="form-group">
                <label class="col-sm-3 control-label">关联用户：</label>
                <div class="col-sm-8">
                    <input id="userNickname" name="userNickname" th:field="*{userNickname}" class="form-control" type="text" onclick="userSelect()">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: summernote-js" />
    <script th:inline="javascript">
        var prefix = ctx + "zsyh/userMessage";
        $("#form-userMessage-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-userMessage-edit').serialize());
            }
        }

        $(function() {
            $('.summernote').each(function(i) {
                $('#' + this.id).summernote({
                    lang: 'zh-CN',
                    callbacks: {
                        onChange: function(contents, $edittable) {
                            $("input[name='" + this.id + "']").val(contents);
                        },
                        onImageUpload: function(files) {
                            var obj = this;
                            var data = new FormData();
                            data.append("file", files[0]);
                            $.ajax({
                                type: "post",
                                url: ctx + "common/uploadOSS",
                                data: data,
                                cache: false,
                                contentType: false,
                                processData: false,
                                dataType: 'json',
                                success: function(result) {
                                    if (result.code == web_status.SUCCESS) {
                                        $('#' + obj.id).summernote('insertImage', result.url);
                                    } else {
                                        $.modal.alertError(result.msg);
                                    }
                                },
                                error: function(error) {
                                    $.modal.alertWarning("图片上传失败。");
                                }
                            });
                        }
                    }
                });
                var content = $("input[name='" + this.id + "']").val();
                $('#' + this.id).summernote('code', content);
            })
        });

        $(function() {
            //获取被选中的option标签
            var vs = $('#messageType  option:selected').val();
            if(vs == "2"){
                $("#userNicknameDiv").show();
            }else {
                $("#userNicknameDiv").hide();
                $("#userId").val('');
            }
        })

        function stateValue() {
            //获取被选中的option标签
            var vs = $('#messageType  option:selected').val();
            if(vs == "2"){
                $("#userNicknameDiv").show();
            }else {
                $("#userNicknameDiv").hide();
                $("#userId").val('');
            }
        }

        function userSelect() {
            var url = ctx+"zsyh/platformUser/messageView/";
            layer.open({
                type: 2,
                area: ['600px', ($(window).height()-50) + 'px'],
                fix: false,
                //不固定
                maxmin: true,
                shade: 0.3,
                title: "选择用户",
                content:  url,
                closeBtn: 1,
                // 弹层外区域关闭
                shadeClose: true,
                end: function(){
                    //location.reload();
                },
                cancel: function(index) {
                    //location.reload();
                }
            });
        }

    </script>
</body>
</html>